package com.lvmama.pangolin.redisutil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.lvmama.pangolin.conf.HbaseRedisSettings;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
//TODO PropertiesConfig先装配
@EnableConfigurationProperties(HbaseRedisSettings.class)
public class JedisTemplateBean {
	/**  
	* @Title：JedisTemplateBean.java
	* @Author:zhoutianqi
	* @Description:
	* @Date：2017年2月10日下午3:47:20  
	*/
	@Autowired
	private HbaseRedisSettings hbaseRedisSettings;
	public JedisTemplate getWriterJedisTemplate(){
		if(!hbaseRedisSettings.getRedisenable().equals("true")){return null;}
		int port = Integer.parseInt(hbaseRedisSettings.getRediswriterport());
		int maxIdle = Integer.parseInt(hbaseRedisSettings.getRediswritermaxIdle());
		int maxTotal = Integer.parseInt(hbaseRedisSettings.getRediswritermaxTotal());
		int checkingIntervalSecs = Integer.parseInt(hbaseRedisSettings.getRediswritercheckingIntervalSecs());
		int evictableIdleTimeSecs = Integer.parseInt(hbaseRedisSettings.getRediswriterevictableIdleTimeSecs());
		int maxWaitMillis = Integer.parseInt(hbaseRedisSettings.getRediswritermaxWaitMillis());
		JedisPoolConfig config = JedisUtils.createPoolConfig(maxIdle, maxTotal, checkingIntervalSecs, evictableIdleTimeSecs, maxWaitMillis);
		config.setTestWhileIdle(true);
		JedisTemplate instance = new JedisTemplate(new JedisPool(config, hbaseRedisSettings.getRediswriterserver(), port));
		System.out.println("JedisTemplateBean jedisWriterTemplate数据："+instance.toString());
		return instance;
	}
	@Bean(name = "jedisReaderTemplate")
	public JedisTemplate getReaderJedisTemplate(){
		if(!hbaseRedisSettings.getRedisenable().equals("true")){return null;}
		int port = Integer.parseInt(hbaseRedisSettings.getRedisreaderport());
		int maxIdle = Integer.parseInt(hbaseRedisSettings.getRedisreadermaxIdle());
		int maxTotal = Integer.parseInt(hbaseRedisSettings.getRedisreadermaxTotal());
		int checkingIntervalSecs = Integer.parseInt(hbaseRedisSettings.getRedisreadercheckingIntervalSecs());
		int evictableIdleTimeSecs = Integer.parseInt(hbaseRedisSettings.getRedisreaderevictableIdleTimeSecs());
		int maxWaitMillis = Integer.parseInt(hbaseRedisSettings.getRedisreadermaxWaitMillis());
		JedisPoolConfig config = JedisUtils.createPoolConfig(maxIdle, maxTotal, checkingIntervalSecs, evictableIdleTimeSecs, maxWaitMillis);
		config.setTestWhileIdle(true);
		JedisTemplate instance = new JedisTemplate(new JedisPool(config, hbaseRedisSettings.getRedisreaderserver(), port));
		System.out.println("JedisTemplateBean jedisReaderTemplate数据："+instance.toString());
		return instance;
	}
}
